package gen.tools.query;

import static java.lang.System.out;
import gen.tools.Widgets;
import gen.tools.structures.S2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryIniInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryIniInfo() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final Widgets wid = new Widgets();
		request.setCharacterEncoding(wid.UIcodeSet);
		response.setCharacterEncoding(wid.UIcodeSet);
		response.setContentType("application/json");
		
		final String pageUrl = request.getParameter("pageName");
		final String empId = request.getParameter("empId");
		final S2 s2 = new S2();
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs= null;
		
		
		
		//ApplicationContext context= null;
		
		try {
			conn = wid.getConn();
			ps = conn.prepareStatement("SELECT pageTitle FROM pagelist WHERE pageUrl=?");
			ps.setString(1,pageUrl);
			rs = ps.executeQuery();
			if( rs.next() ) {
				s2.setS1( rs.getString("pageTitle") );
			}
			
			ps = conn.prepareStatement("SELECT empName FROM emps WHERE empId=?");
			ps.setString(1,empId);
			rs = ps.executeQuery();
			if( rs.next() ) {
				s2.setS2( rs.getString("empName") );
			}
			
			/*context= new ClassPathXmlApplicationContext("springContext.xml");
			//要JdbcTemplate Bean
			
			JdbcTemplate jdbcTemplate = context.getBean("template", JdbcTemplate.class );
			String sql = "SELECT pageTitle FROM pagelist WHERE pageUrl=?";	

			
			List<String> results = jdbcTemplate.query(sql,
					new Object[] { pageUrl }, new RowMapper<String>() {
						public String mapRow(ResultSet rs, int rowNum) throws SQLException {
							return rs.getString("pageTitle");
						}
					});
			s2.setS1( results.get(0) );
			
			sql = "SELECT empName FROM emps WHERE empId=?";
			results = jdbcTemplate.query(sql,
					new Object[] { empId }, new RowMapper<String>() {
						public String mapRow(ResultSet rs, int rowNum) throws SQLException {
							return rs.getString("empName");
						}
					});
			s2.setS2( results.get(0) );*/
			
		} catch (Exception e) {
			wid.errLog(e, pageUrl+"UUU"+empId);
		} finally {
			//((ConfigurableApplicationContext)context).close();
			
			try {
				if ( rs !=null ) {rs.close();}
				if ( ps !=null ) {ps.close();}
				if ( conn !=null ) {conn.close();}	
			} catch (Exception e1) {
				out.println( wid.errProcess(e1) );
			}
		}
		
		response.getWriter().write( new Gson().toJson(s2) );
	}

}
